/**
 * Created by zhourh on 2018/5/25.
 *
 * 不使用运算符 + 和-，计算两整数a 、b之和。

 示例：
 若 a = 1 ，b = 2，返回 3。
 *
 * 递归&位运算
 */
public class SumOfTwoInteger {

    public static void main(String[] args) {
        SumOfTwoInteger sumOfTwoInteger = new SumOfTwoInteger();
        System.out.println("-1 + -2 = " + sumOfTwoInteger.getSum(-1, -2));
        System.out.println("-9 + -18 = " + sumOfTwoInteger.getSum(-9, -18));
        System.out.println("-12 + 88 = " + sumOfTwoInteger.getSum(-12, 88));
        System.out.println("-1 + 1 = " + sumOfTwoInteger.getSum(-1, 1));
        System.out.println("1 + 2 = " + sumOfTwoInteger.getSum(1, 2));
        System.out.println("11 + 12 = " + sumOfTwoInteger.getSum(11, 12));
        System.out.println("832 + 322 = " + sumOfTwoInteger.getSum(832, 322));

    }

    public int getSum(int a, int b) {
        if ((a & b) == 0) {
            return a | b;
        }
        return getSum(a ^ b, (a & b) << 1);
    }

}



















